package com.netty.rpc.loadbalance;

import com.netty.rpc.common.entity.NettyClient;
import com.netty.rpc.registar.RpcClientEntity;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;


public class RoundRobinLoadBalaceExecutor extends AbstractLoadBalanceExecutor {

    private Map<String, AtomicLong> indexMap = Maps.newConcurrentMap();

    @Override
    protected RpcClientEntity loadBalance(RpcClientEntity rpcClientEntity, String interfaceName, List<NettyClient> candidates) {
        AtomicLong atomicLong = indexMap.get(interfaceName);
        if (atomicLong == null) {
            atomicLong = new AtomicLong(0);
            indexMap.put(interfaceName, atomicLong);
        }
        int index = (int) (atomicLong.getAndAdd(1) % candidates.size());
        NettyClient nettyClient = candidates.get(Math.abs(index));
        RpcClientEntity entity = new RpcClientEntity();
        BeanUtils.copyProperties(nettyClient, entity);
        entity.setIsSyn(rpcClientEntity.isSyn());
        entity.setRpcClz(rpcClientEntity.getRpcClz());
        return entity;
    }
}
